package com.hzjy.manage.controller;

import java.util.List;

import com.hzjy.api.beans.HzjyProduct;
import com.hzjy.common.core.domain.model.LoginUser;
import com.hzjy.common.utils.PageData;
import com.hzjy.common.utils.ServletUtils;
import com.hzjy.framework.web.service.TokenService;
import com.hzjy.manage.service.AuditDelistService;
import com.hzjy.manage.service.HzjyDelistService;
import com.hzjy.manage.vo.AuditDelist;
import com.hzjy.manage.vo.HzjyDelist;
import com.hzjy.manage.vo.HzjyDelistVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;

/**
 * 摘牌审核记录-初审
 *
 * @author hzjy
 * @date 2020-08-23
 */
@Api("初审")
@RestController
@RequestMapping("/manage/audit_delist_data_first")
public class AuditDelistDataFirstController extends BaseController
{
    @Autowired
    private AuditDelistService auditDelistService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private HzjyDelistService hzjyDelistService;

    /**
     * 查询摘牌审核记录列表
     */
    @ApiOperation("获取待审核集合")
    @PreAuthorize("@ss.hasPermi('manage:audit_delist_data_first:list')")
    @PostMapping("/list")
    public TableDataInfo list(HzjyDelistVO hzjyDelistVO)
    {
        startPage();
        List<HzjyDelistVO> hzjyDelists = hzjyDelistService.selectHzjyDelistList(hzjyDelistVO);
        return getDataTable(hzjyDelists);
    }


    /**
     * 获取摘牌审核记录详细信息
     */
    @ApiOperation("查看详情")
    @PreAuthorize("@ss.hasPermi('manage:audit_delist_data_first:select')")
    @PostMapping("/get_audit_delis")
    public AjaxResult getAuditDelis(@RequestBody HzjyDelist hzjyDelist)
    {
        return AjaxResult.success(hzjyDelistService.selectHzjyDelistById(hzjyDelist.getDelistId()));
    }


    /**
     * 初审通过
     * @param auditDelist
     * @return
     */
    @ApiOperation("初审通过")
    @PreAuthorize("@ss.hasPermi('manage:audit_delist_data_first:agree')")
    @PostMapping("/agree_audit_delis")
    public AjaxResult agreeAuditDelis(@RequestBody AuditDelist auditDelist)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        HzjyDelistVO hzjyDelist = hzjyDelistService.selectHzjyDelistById(auditDelist.getDelistId());
        auditDelist.setUserId(loginUser.getUser().getUserId());
        auditDelist.setFirstState(hzjyDelist.getStatus());
        //修改状态值
        auditDelistService.agreeAuditDelis(auditDelist);

        //查询修改后的状态
        HzjyDelistVO newHzjyDelist = hzjyDelistService.selectHzjyDelistById(auditDelist.getDelistId());
        //插入消息通知
        auditDelistService.insertDelistNotice(newHzjyDelist);
        return AjaxResult.success();
    }


    /**
     * 初审通过
     * @param auditDelist
     * @return
     */
    @ApiOperation("初审驳回")
    @PreAuthorize("@ss.hasPermi('manage:audit_delist_data_first:refuse')")
    @PostMapping("/refuse_audit_delis")
    public AjaxResult refuseAuditData(@RequestBody AuditDelist auditDelist)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        HzjyDelistVO hzjyDelist = hzjyDelistService.selectHzjyDelistById(auditDelist.getDelistId());
        auditDelist.setUserId(loginUser.getUser().getUserId());
        auditDelist.setFirstState(hzjyDelist.getStatus());
        //修改状态值
        auditDelistService.refuseAuditData(auditDelist);

        //查询修改后的状态
        HzjyDelistVO newHzjyDelist = hzjyDelistService.selectHzjyDelistById(auditDelist.getDelistId());
        //插入消息通知
        auditDelistService.insertDelistNotice(newHzjyDelist);
        return AjaxResult.success();
    }

    /**
     * 产品详情页(所有产品的详情页)
     */
    @ApiOperation("产品详情页")
    @PreAuthorize("@ss.hasPermi('manage:info:selectOne')")
    @PostMapping("/info")
    public AjaxResult addProduct(@RequestBody PageData pd) {
        Object productId = pd.get("productId");
        HzjyProduct hzjyProduct = new HzjyProduct();
        hzjyProduct.setProductId(Long.valueOf(productId.toString()));
        return AjaxResult.success(200, "查询成功", hzjyDelistService.queryProductInfo(hzjyProduct));
    }

}
